package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.LsLawyerArticleContent;
import com.ruoyi.system.domain.LsLawyerArts;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.LsLawyerArticle;
import com.ruoyi.system.service.ILsLawyerArticleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 文章Controller
 *
 * @author zhouyipeng
 * @date 2023-10-16
 */
@RestController
@RequestMapping("/system/article")
public class LsLawyerArticleController extends BaseController
{
    @Autowired
    private ILsLawyerArticleService lsLawyerArticleService;

    /**
     * 查询文章列表
     */
    @PreAuthorize("@ss.hasPermi('system:article:list')")
    @GetMapping("/list")
    public TableDataInfo list(LsLawyerArticle lsLawyerArticle)
    {
        startPage();
        List<LsLawyerArticle> list = lsLawyerArticleService.selectLsLawyerArticleList(lsLawyerArticle);
        return getDataTable(list);
    }

    /**
     * 导出文章列表
     */
    @PreAuthorize("@ss.hasPermi('system:article:export')")
    @Log(title = "文章", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LsLawyerArticle lsLawyerArticle)
    {
        List<LsLawyerArticle> list = lsLawyerArticleService.selectLsLawyerArticleList(lsLawyerArticle);
        ExcelUtil<LsLawyerArticle> util = new ExcelUtil<LsLawyerArticle>(LsLawyerArticle.class);
        util.exportExcel(response, list, "文章数据");
    }

    /**
     * 获取文章详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:article:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(lsLawyerArticleService.selectLsLawyerArticleById(id));
    }

    /**
     * 新增文章
     */
    @PreAuthorize("@ss.hasPermi('system:article:add')")
    @Log(title = "文章", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LsLawyerArticleContent lsLawyerArticleContent)
    {
        return toAjax(lsLawyerArticleService.insertLsLawyerArticle(lsLawyerArticleContent));
    }

    /**
     * 修改文章
     */
    @PreAuthorize("@ss.hasPermi('system:article:edit')")
    @Log(title = "文章", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LsLawyerArticleContent lsLawyerArticleContent)
    {
        if(lsLawyerArticleContent.getId()!=null &&!"".equals(lsLawyerArticleContent.getId())){
            return toAjax(lsLawyerArticleService.updateLsLawyerArticle(lsLawyerArticleContent));
        }else{
            return error("修改文章id不能为空");
        }

    }

    /**
     * 删除文章
     */
    @PreAuthorize("@ss.hasPermi('system:article:remove')")
    @Log(title = "文章", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(lsLawyerArticleService.deleteLsLawyerArticleByIds(ids));
    }

    /**
     * 查询每个分组中前五条最新的数据
     */
    //@PreAuthorize("@ss.hasPermi('system:article:query')")
    @PostMapping("/queryArts")
    public TableDataInfo queryTitleByType()
    {
        List<LsLawyerArts> list = lsLawyerArticleService.queryLawyerArticle();
        return getDataTable(list);
    }
}
